
#include <stdio.h>
#include <string.h>

int pmt[64] = {0};

int* get_pmt(char * s)
{
    int len = strlen(s);
    int i = 0;
    int j = -1;
    pmt[0] = 0;
    while (i < len && j < len) {
        if (j == -1 || s[i] == s[j]) {
            ++i;
            ++j;
            pmt[i - 1] = j;
        } else {
            j = -1;
        }
    }
}

int kmp_match(char* p, char* s)
{
    int plen = strlen(p);
    int slen = strlen(s);
    int i = 0, j = 0;

    while (i < plen && j < slen) {
        if (p[i] == s[j]) {
            ++i;
            ++j;
        } else {
            if (j == 0) {
                ++i;
            } else {
                j = pmt[j - 1];
            }
        }
    }
    if (j == slen)
        return i - j;
    else 
        return -1;
}

int main()
{
    char* p = "aacaabc";
    char* s = "abaaab";
    int* k = get_pmt(s);

    int i = 0;
    for (i = 0; i < strlen(s); i++) {
        printf("%d ", pmt[i]);
    }


    //int res = kmp_match(p ,s);
    //printf("\nres : %d\n", res);
    return 0;


}




